//
//  BDSearchResultsViewController.m
//  BadBoy
//
//  Created by wen on 2017/4/7.
//  Copyright © 2017年 com.8xiong. All rights reserved.
//

#import "BDSearchResultsViewController.h"
#import "BDUserModel.h"
#import "BDLocationServer.h"
#import <AMapSearchKit/AMapSearchKit.h>

@interface BDSearchResultsViewController ()<UITableViewDelegate, UITableViewDataSource, AMapSearchDelegate>
{
    NSString *currentKeyword;
}
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * datasource;
@property (nonatomic, strong) AMapSearchAPI *search;

@end

@implementation BDSearchResultsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)requestDataWithKeyword:(NSString *)keyword{
    // BDLog(ASLocalizedString(@"根据关键字%@来查询商家"), keyword);
    if ([keyword isNotEmpty]) {
        currentKeyword = keyword;
        self.startPage = @"1";
        [self.datasource removeAllObjects];
        [self requestData];
    }else{
        [self.datasource removeAllObjects];
        [self.tableView reloadData];
    }
}

- (void)requestData{
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
    request.keywords = currentKeyword;
    request.city = @"广州";
    request.types = @"风景名胜|商务住宅|政府机构及社会团体|交通设施服务|公司企业|道路附属设施|地名地址信息";
    //    request.sortrule = 0;
    //    request.requireExtension = YES;
    request.page = self.startPage.integerValue;
    [self.search AMapPOIKeywordsSearch:request];
}

#pragma mark -------------------------AMapSearch代理---------------------------------
- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
    if (response.pois.count == 0) {
        [self.datasource removeAllObjects];
        [self.tableView reloadData];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kBDScreenWidth, 40)];
        label.textColor = [UIColor colorWithHexString:@"#767676"];
        label.font = [UIFont systemFontOfSize:14];
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        label.text = ASLocalizedString(@"以上是所有内容");
        self.tableView.tableFooterView = label;
        return;
    }else{
        self.tableView.tableFooterView = nil;
    }
    
    [self.datasource addObjectsFromArray:response.pois];
    [self.tableView reloadData];
    
    self.startPage = [NSString stringWithFormat:@"%ld", (self.startPage.integerValue + 1)];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"123"];
    }
    AMapPOI *poi = self.datasource[indexPath.row];
    cell.textLabel.text = poi.name;
    cell.textLabel.textColor = [UIColor colorWithHexString:@"#1c1c1c"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.text = poi.address;
    cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#767676"];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    
    cell.layer.borderColor = kBDColorGray.CGColor;
    cell.layer.borderWidth = 0.5;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [[NSNotificationCenter defaultCenter] postNotificationName:SearchResultsControllerDidSelectRow object:self.datasource[indexPath.row]];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (UITableView *)tableView{
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kBDTopHeight, kBDScreenWidth, kBDScreenHeight - kBDTopHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
        }];
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)datasource{
    if (!_datasource) {
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}

- (AMapSearchAPI *)search{
    if (!_search) {
        self.search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
